package com.wd.live.view


import androidx.fragment.app.FragmentPagerAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
//import com.alibaba.android.arouter.facade.annotation.Route

import com.google.android.material.tabs.TabLayout
import com.wd.live.R
import com.wd.live.databinding.ActivityMainBinding
import com.wd.live.module_base.net.util.MMKVUtil
import com.wd.live.module_list.ListFragment
import com.wd.live.module_live.view.fragment.LiveFragment
import com.wd.live.module_my.view.fragment.MyFragment
import com.wd.live.module_nearby.NearbyFragment
import com.wd.live.module_wide.view.fragment.WideFragment
import com.wd.live.module_base.ui.base.BaseActivity
import com.wd.live.module_base.ui.base.BaseViewModel
@Route(path = "/main/main_activity")
class MainActivity : BaseActivity<BaseViewModel, ActivityMainBinding>() {
    val image= mutableListOf<Int>(R.mipmap.app_square_0,R.mipmap.app_position_page_0,R.mipmap.app_broadcast_icon,R.mipmap.app_list_selected_0,R.mipmap.app_my_choice_0)
    val defaultImage= mutableListOf<Int>(R.mipmap.app_square,R.mipmap.app_position_page_0,R.mipmap.app_broadcast_icon,R.mipmap.app_list_selected_0,R.mipmap.app_my_choice_0)
    val selectImage= mutableListOf<Int>(R.mipmap.app_square,R.mipmap.app_position_page,R.mipmap.app_broadcast_icon,R.mipmap.app_list_selected,R.mipmap.app_my_choice)
    val titleArr= mutableListOf<String>("广场","附近","","榜单","我的")
    var isLogin:Boolean=false
    override fun getLayout(): Int {
        return R.layout.activity_main
    }
    override fun initData() {
         isLogin = MMKVUtil.getIsLogin()
        if (!isLogin){
            ARouter.getInstance().build("/login/login_activity").navigation()
            finish()
        }
        //创建fragment集合
        val fragment= mutableListOf(
            WideFragment(), NearbyFragment(), LiveFragment(),
            ListFragment(), MyFragment()
        )
        //预加载
        binding.mainVp.offscreenPageLimit=fragment.size-1
        binding.mainVp.adapter=object : FragmentPagerAdapter(supportFragmentManager){
            //集合总数量
            override fun getCount()=fragment.size
            //fragment布局
            override fun getItem(position: Int)=fragment[position]
            //标题
            override fun getPageTitle(position: Int)= titleArr[position]
        }
        //绑定viewPage
        binding.mainTb.setupWithViewPager(binding.mainVp)
        //设置选中图片
//        binding.mainTb.getTabAt(0)?.setIcon(R.mipmap.app_square)
//        binding.mainTb.getTabAt(1)?.setIcon(R.mipmap.app_position_page)
//        binding.mainTb.getTabAt(2)?.setIcon(R.mipmap.app_broadcast_icon)
//        binding.mainTb.getTabAt(3)?.setIcon(R.mipmap.app_list_selected)
//        binding.mainTb.getTabAt(4)?.setIcon(R.mipmap.app_my_choice)
        //默认初始化
        for ((index, i) in defaultImage.withIndex()) {
            binding.mainTb.getTabAt(index)?.setIcon(defaultImage[index])
        }

        binding.mainTb.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                //选中监听
                if (tab != null) {
                    tab.setIcon(selectImage[tab.position])
                }
            }
            //未选中监听
            override fun onTabUnselected(tab: TabLayout.Tab?) {
                if (tab != null) {
                    tab.setIcon(image[tab.position])
                }
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }
        })


//        //第一步获取到NavHostFragment
//        val host: NavHostFragment = supportFragmentManager.findFragmentById(R.id.fragment_page) as NavHostFragment
//        //第二步获取到NavController
//        val navController = host.navController
//                //默认初始化
//        for ((index, i) in defaultImage.withIndex()) {
//            binding.mainTb.addTab(binding.mainTb.newTab().setIcon(defaultImage[index]))
//            binding.mainTb.getTabAt(index)?.setText(titleArr[index])
//        }
//        binding.mainTb.getTabAt(0)?.select();
//        binding.mainTb.addOnTabSelectedListener(object :TabLayout.OnTabSelectedListener{
//            override fun onTabSelected(tab: TabLayout.Tab?) {
//                when(tab?.position){
//                    0-> navController.navigate(R.id.wideFragment)
//                    1-> navController.navigate(R.id.nearbyFragment)
//                    2-> navController.navigate(R.id.liveFragment)
//                    3-> navController.navigate(R.id.listFragment)
//                    4-> navController.navigate(R.id.myFragment)
//                }
//                if (tab != null) {
//                    tab.setIcon(selectImage[tab.position])
//                };
//            }
//
//            override fun onTabUnselected(tab: TabLayout.Tab?) {
//                if (tab != null) {
//                    tab.setIcon(image[tab.position])
//                };
//            }
//
//            override fun onTabReselected(tab: TabLayout.Tab?) {
//                TODO("Not yet implemented")
//            }
//        })
    }

    override fun getViewModelClass(): Class<BaseViewModel> {
        return BaseViewModel::class.java
    }

}